﻿@model ProductModel.ProductVariantAttributeValueListModel
@using Telerik.Web.Mvc.UI;
@using SmartStore.Web.Framework.UI;
@using SmartStore.Admin.Extensions;
@{    
    ViewBag.Title = string.Format(T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.EditAttributeDetails").Text, Model.ProductVariantAttributeName, Model.ProductName);
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "productvariantattribute-form" }))
{
	<div class="section-header">
		<div class="title">
			@string.Format(T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.EditAttributeDetails").Text, Model.ProductVariantAttributeName, Model.ProductName)
			@Html.ActionLink("(" + T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.BackToProduct") + ")", "Edit", "Product", new { Id = Model.ProductId }, null)
		</div>
		<div class="options">
		</div>
	</div>

	<div>
		@(Html.Telerik().Grid<ProductModel.ProductVariantAttributeValueModel>()
			.Name("pvav-grid")
			.DataKeys(x =>
			{
				x.Add(y => y.Id).RouteKey("pvavId");
				x.Add(y => y.ProductVariantAttributeId).RouteKey("productVariantAttributeId");
			})
			.Columns(columns =>
			{
				columns.Bound(x => x.Name)
					.ReadOnly()
					.ClientTemplate(@Html.VariantAttributeValueName());
				columns.Bound(x => x.Alias);
				columns.Bound(x => x.LinkedProductName)
					.ReadOnly()
					.ClientTemplate(@Html.LabeledProductName("LinkedProductId", "LinkedProductName", "LinkedProductTypeName", "LinkedProductTypeLabelHint"));
				columns.Bound(x => x.PriceAdjustmentString)
					.Width(80)
					.Centered();
				columns.Bound(x => x.WeightAdjustmentString)
					.Width(80)
					.Centered();
				columns.Bound(x => x.IsPreSelected)
					.Width(80)
					.ClientTemplate(@Html.SymbolForBool("IsPreSelected"))
					.Centered();
				columns.Bound(x => x.DisplayOrder)
					.Width(80)
					.Centered();
				columns.Command(commands =>
				{
					commands.Custom("edit-pvav").Text(T("Common.Edit"));
					commands.Delete().Localize(T);
				})
				.HtmlAttributes(new { align = "right" })
				.Width(150);
			})
			.DataBinding(dataBinding => dataBinding.Ajax()
				.Select("ProductAttributeValueList", "Product", new { productVariantAttributeId = Model.ProductVariantAttributeId })
				.Delete("ProductAttributeValueDelete", "Product"))
			.ToolBar(commands => commands.Template(GridCommands))
			.EnableCustomBinding(true))
	</div>
}

@helper GridCommands(Grid<ProductModel.ProductVariantAttributeValueModel> grid)
{
	<button class="t-button t-button-primary" type="submit" id="btnAddNewValue" name="btnAddNewValue" value="btnAddNewValue"
			onclick="javascript:openPopup('@(Url.Action("ProductAttributeValueCreatePopup", "Product", new { productVariantAttributeId = Model.ProductVariantAttributeId, btnId = "btnRefresh", formId = "productvariantattribute-form" }))'); return false;">
		<i class="fa fa-plus"></i>
		<span>@T("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.AddNew")</span>
	</button>

	<button type="submit" id="btnRefresh" name="btnRefresh" value="btnRefresh" class="d-none" />
}


<script>
	$(function () {
        $('#btnRefresh').on('click', function () {
            // refresh grid
            $('#pvav-grid').data('tGrid').ajaxRequest();
            // return false to don't reload a page
            return false;
        });

		$('#pvav-grid').on('click', '.t-grid-edit-pvav', function (e) {
			e.preventDefault();
			var grid = $('#pvav-grid').data('tGrid');
			var tr = $(this).closest('tr');
			var id = grid.dataItem(tr).Id;
			var href = '@Url.Content("~/Admin/Product/ProductAttributeValueEditPopup/")' + id + '?btnId=btnRefresh&formId=productvariantattribute-form';

			openPopup(href);

			return false;
		});
    });
</script>
